package com.sixmac.utils;

import com.sixmac.core.constant.ConstConfig;
import com.sixmac.module.vo.FileVo;
import org.apache.commons.io.FileUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Blade on 2017/5/8 0008.
 */
public class FileUploadUtil {

    /**
     * 上传图片
     *
     * @param file
     * @return
     */
    public static String uploadFile(MultipartFile file) {
        if (null == file) {
            return null;
        }
        String filePath = null;
        String name = String.valueOf(System.currentTimeMillis());
        String originalFileName = file.getOriginalFilename().toLowerCase();
        String fileType = originalFileName.substring(originalFileName.lastIndexOf("."));

        try {
            InputStream inputStream = file.getInputStream();
            String origFileName = file.getOriginalFilename(); //原始名称,如aa.jpg
            String ext = FileUtil.getFileExt(origFileName); //后缀，如jpg
            String uploadPath;
            if (fileType.toLowerCase().equalsIgnoreCase(".mp4")) {
                uploadPath = UploadUtil.getVideosUpladPath(); //生成日期目录
            } else {
                uploadPath = UploadUtil.getImagesUpladPath(); //生成日期目录 image/2014/7/
            }
            String desFilePathName = uploadPath + name + ext;//完整文件名称 image/2014/7/221144144554.jpg
            File theFile = new File(ConstConfig.UPLOADFILE_PATH, desFilePathName); //生成的文件对象

            FileUtils.copyInputStreamToFile(inputStream, theFile);
            filePath = desFilePathName;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return filePath;
    }


    public static String uploadGoodsImg(MultipartFile file) {
        if (null == file) {
            return null;
        }
        String filePath = null;

        try {
            InputStream inputStream = file.getInputStream();
            String origFileName = file.getOriginalFilename(); //原始名称,如aa.jpg
            String uploadPath;
            uploadPath = "img/"; //生成日期目录 goodsImage/2014/7/
            String desFilePathName = uploadPath + origFileName;
            File theFile = new File(ConstConfig.UPLOADFILE_PATH, desFilePathName); //生成的文件对象

            FileUtils.copyInputStreamToFile(inputStream, theFile);
            filePath = desFilePathName;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return filePath;
    }

    //批量上传图片
    public static List<FileVo> uploadImgs(List<MultipartFile> multipartFileList) {
        List<FileVo> fileVoList = new ArrayList<>();

        if (CollectionUtils.isEmpty(multipartFileList)) {
            return fileVoList;
        }
        // 在获取的系统时间后面加上数组下标，防止会有同名的图片生成
        int i = 0;

        for (MultipartFile file : multipartFileList) {
            FileVo fileVo = new FileVo();
            if (null == file) {
                continue;
            }
            String filePath = null;
            String name = String.valueOf(System.currentTimeMillis()) + i;
            String originalFileName = file.getOriginalFilename().toLowerCase();
            String fileType = originalFileName.substring(originalFileName.lastIndexOf("."));

            try {
                InputStream inputStream = file.getInputStream();
                String origFileName = file.getOriginalFilename(); //原始名称,如aa.jpg
                String ext = FileUtil.getFileExt(origFileName); //后缀，如jpg
                String uploadPath;
                if (fileType.toLowerCase().equalsIgnoreCase(".mp4")) {
                    uploadPath = UploadUtil.getVideosUpladPath(); //生成日期目录
                } else {
                    uploadPath = UploadUtil.getImagesUpladPath(); //生成日期目录 image/2014/7/
                }
                String desFilePathName = uploadPath + name + ext;//完整文件名称 image/2014/7/221144144554.jpg
                File theFile = new File(ConstConfig.UPLOADFILE_PATH, desFilePathName); //生成的文件对象

                FileUtils.copyInputStreamToFile(inputStream, theFile);
                filePath = desFilePathName;

            } catch (IOException e) {
                e.printStackTrace();
            }

            fileVo.setName(name);
            fileVo.setPath(filePath);
            fileVoList.add(fileVo);
        }
        return fileVoList;
    }
}
